/** *******************************************************************
 * FlexDatabaseManager
 * Copyright (c) 2008 Jonnie Spratley. All Rights Reserved.
 * @author  Jonnie Spratley
 * @contact jonniespratley@gmail.com
 * @internal
 ******************************************************************* */
package com.jonniespratley.flexdatabasemanager.vo
{
	import com.jonniespratley.flexdatabasemanager.view.assets.AssetManager;
	
	import mx.collections.ArrayCollection;
	import mx.utils.UIDUtil;
	
	[Bindable]
	public class TableVO extends Object
	{
		public var tableName:String;
		public var aTable:String;
		public var aKey:String;
		public var aType:String;
		public var aIcon:String;
		public var aData:String;
		public var aFields:ArrayCollection;
		public var tableUUID:String;
		
		public var aStatus:TableStatusVO;
		public var aIndexes:ArrayCollection;		
		
		public function  TableVO( obj:Object = null )
		{	
			if ( obj != null )
			{
				this.tableName = obj['aTable'];
				
				this.aTable = obj['aTable'];
				this.aKey = obj['aKey'];
				this.aType = obj['aType'];
				this.aIcon = obj['aIcon'];
				this.aData = obj['aData'];
				
				/* ***********************************************
				 * Field parsing
				 *********************************************** */
				 
				//set the fields comming back as an Array 
				var tempFieldArray:Array = obj['aFields'] as Array;
				
				//create a temp arraycollection so return to the aFields
				var tempFieldAC:ArrayCollection = new ArrayCollection();
				
				//loop through every string in the fields array
				for ( var fString:String in tempFieldArray )
				{	
					//for each object add it to the temp collection
					tempFieldAC.addItem( new FieldVO( tempFieldArray[ fString ] ) );	
				}
				//set the array collection to the build one of value objects
				this.aFields = tempFieldAC;
				
				/* ***********************************************
				 * Index parsing
				 *********************************************** */
				 
				//set the fields comming back as an Array 
				var tempIndexArray:Array = obj['aIndexes'] as Array;
				
				//create a temp arraycollection so return to the aFields
				var tempIndexAC:ArrayCollection = new ArrayCollection();
				
				//loop through every string in the fields array
				for ( var iString:String in tempIndexArray )
				{	
					//for each object add it to the temp collection
					tempIndexAC.addItem( new IndexVO( tempIndexArray[ iString ] ) );	
				}
				//set the array collection to the build one of value objects
				this.aIndexes = tempIndexAC;
				
				
				/* ***********************************************
				 * Index parsing
				 *********************************************** */
				 
				//set the fields comming back as an Array 
				var tempStatusArray:Array = obj['aStatus'] as Array;				
				this.aStatus = new TableStatusVO( tempStatusArray[ 0 ] );

			} 
		}
		
		public function get label():String
		{
			return aTable;
		}
		
		public function get children():Array
		{
			return aFields.toArray();
		}
		
		
		public function get index():Array
		{
			return aIndexes.toArray();
		}
		
		public function get data():Object
		{
			return aType;
		}
		
		public function get key():String
		{
			return aKey;
		}
		
		public function get icon():Class
		{
			return AssetManager.voTableIcon;
		}
		
		public function get type():String
		{
			return 'table';
		}
		
		public function get media():String
		{
			return 'assets/images/Table.swf';
		}
		
		public function createUUID():String
		{
			return UIDUtil.createUID();
		}
		
	}
}